package com.wk.springboot.config;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.servlet.Filter;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.cas.CasRealm;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.jasig.cas.client.session.SingleSignOutFilter;
import org.jasig.cas.client.session.SingleSignOutHttpSessionListener;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

/**
 * @Classname LoginAutoConfigure
 * @Description login的配置类
 * @Date 2020/8/14 17:12
 * @Author by weikai
 */

@Configuration
public class LoginAutoConfigure {
  /**
   * 实现单点登出功能
   * 单点登出过滤器
   * @author jiangyishan
   * @return FilterRegistrationBean
   */
  @Bean
  @ConditionalOnProperty("artery.login.ssoServer")
  @ConditionalOnClass(SingleSignOutFilter.class)
  public FilterRegistrationBean singleSignOutFilter() {
    FilterRegistrationBean<SingleSignOutFilter> registration = new FilterRegistrationBean<>();
    registration.setFilter(new SingleSignOutFilter());
    registration.addUrlPatterns("/*");
    registration.setOrder(1);
    registration.setName("singleSignOutFilter");
    return registration;
  }

  /**
   * 实现单点登出功能
   * 单点登出session监听器
   * @author jiangyishan
   * @return ServletListenerRegistrationBean
   */
  @Bean
  @ConditionalOnProperty("artery.login.ssoServer")
  @ConditionalOnClass(SingleSignOutFilter.class)
  public ServletListenerRegistrationBean singleSignOutHttpSessionListener() {
    return new ServletListenerRegistrationBean<>(new SingleSignOutHttpSessionListener());
  }

/*   @Bean
  @ConditionalOnMissingBean
  public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
    return new LifecycleBeanPostProcessor();
  }

 @Bean
  @ConditionalOnMissingBean
  @DependsOn("lifecycleBeanPostProcessor")
  @ConditionalOnProperty(name = "spring.aop.auto", havingValue = "false")
  // spring已经开启动态代理，如果在注册此切面会导致多次代理，而报错 => http://code.thunisoft.com/artery/artery6/issues/35
  public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
    return new DefaultAdvisorAutoProxyCreator();
  }

  @Bean
  @ConditionalOnMissingBean
  public AuthorizationAttributeSourceAdvisor AuthorizationAttributeSourceAdvisor(SecurityManager manager) {
    AuthorizationAttributeSourceAdvisor aasa = new AuthorizationAttributeSourceAdvisor();
    aasa.setSecurityManager(manager);
    return aasa;

  }*/
}
